package com.car.loan.project.xr.controller.after;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.loan.common.file.ExcelExportUtil;
import com.car.loan.framework.aspectj.lang.annotation.Log;
import com.car.loan.framework.aspectj.lang.enums.BusinessType;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.controller.ExcelFileUtil;
import com.car.loan.project.xr.service.UrgentService;
import com.car.loan.project.xr.util.bo.business.after.OverdueUrgentLogBo;
import com.car.loan.project.xr.util.vo.after.OverdueUrgentLogInfo;
import com.car.loan.project.xr.util.bo.business.after.OverdueUrgentLogPageParam;
import com.car.loan.project.xr.util.vo.after.OverdueUrgentLogVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.InputStream;
import java.time.LocalDate;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author huangding
 * @date 2020/2/21 20:47
 */
@RestController
@RequestMapping("xr/urgent")
@Api(value = "催收管理", tags = "贷后管理-催收管理")
public class UrgentController {

    @Autowired
    private UrgentService urgentService;


    @Resource
    private ExcelFileUtil excelFileUtil;


    @Autowired
    protected HttpServletRequest request;

    @PreAuthorize("@ss.hasPermi('overdue:urgent:list')")
    @GetMapping("list")
    @ApiOperation(value = "逾期催收列表", responseContainer = "list", response = OverdueUrgentLogVo.class)
    public AjaxResult list(@Valid OverdueUrgentLogPageParam overdueUrgentLogPageParam) {
        return AjaxResult.success(urgentService.getOverdueUrgentLogPage(overdueUrgentLogPageParam));
    }


    @PreAuthorize("@ss.hasPermi('overdue:urgent:list')")
    @GetMapping("excel")
    @ApiOperation("逾期催收列表-excel")
    public AjaxResult overdueImportLogExcel(
        @Valid OverdueUrgentLogPageParam overdueUrgentLogPageParam,
        HttpServletResponse response) {
        overdueUrgentLogPageParam.setPageNum(1);
        overdueUrgentLogPageParam.setPageSize(99999999);
        Page<OverdueUrgentLogVo> overdueUrgentLogPage = urgentService
            .getOverdueUrgentLogPage(overdueUrgentLogPageParam);
        InputStream templateExcelFile = excelFileUtil
            .getTemplateInputStream("overdue_urgent.xlsx");
        String exportFileName = "逾期催收记录" + LocalDate.now() + ".xlsx";
        ExcelExportUtil
            .exportByBeans(2, templateExcelFile, exportFileName, false, null,
                overdueUrgentLogPage.getRecords(),
                request, response);
        return null;
    }


    @PreAuthorize("@ss.hasPermi('overdue:urgent:query')")
    @GetMapping("{loanId}")
    @ApiOperation(value = "逾期催收详情", response = OverdueUrgentLogInfo.class)
    public AjaxResult info(@PathVariable Long loanId) {
        return AjaxResult.success(urgentService.info(loanId));
    }

    @PreAuthorize("@ss.hasPermi('overdue:urgent:input')")
    @PostMapping("input")
    @ApiOperation(value = "逾期催收录入")
    @Log(title = "逾期催收录入", businessType = BusinessType.INSERT)
    public synchronized AjaxResult input(
        @RequestBody @Valid OverdueUrgentLogBo overdueUrgentLogBo) {
        return urgentService.input(overdueUrgentLogBo);
    }


//    @PreAuthorize("@ss.hasPermi('overdue:urgent:input')")
//    @PostMapping("repayment/{loanId}")
//    @ApiOperation(value = "逾期催收-已还款按钮")
//    @Log(title = "逾期催收-已还款按钮", businessType = BusinessType.UPDATE)
//    public AjaxResult repayment(@PathVariable Long loanId) {
//        return urgentService.repayment(loanId);
//    }


}
